<?php
class wordtagService extends commonService{
	public function __construct(){
		parent::__construct();
		$this->taginfo=array();
		
		$this->cache_dir=PROJECT_PATH.'runtime/wordtag_cache';
		$this->cache_time=3600*4;//4小时
		
		$this->table_index=table('index_2');
		$this->table_list=table('wordtaglist');
		$this->table_arc=table('arc');
	}
	//检测tag是否存在
	public function is_exist($tag){
		$query="select * from `{$this->table_index}` where `tag`='{$tag}' limit 1";
		$r=$this->m->select($query);
		if($r['id']>0){
			$this->taginfo=$r;
			return true;
		}
		return false;
	}
	public function update_hits(){
	
		$id=$this->taginfo['id'];
		if(!$id) return false;
		
		$t=86400;
		$now=time();
		
		$query="update `{$this->table_index}` set `hits`=`hits`+1,`w`=`w`+1,`m`=`m`+1 where `id`='{$id}' limit 1";
		$this->m->query($query);
	
		//周统计
		if(ceil(($now-$this->taginfo['w_up'])/$t)>7){
			$query="update `{$this->table_index}` set `w`='0',`w_up`='{$now}' where `id`='{$id}' limit 1";
			$this->m->query($query);
		}
		//月统计
		if(ceil(($now-$this->taginfo['m_up'])/$t)>30){
			$query="update `{$this->table_index}` set `m`='0',`m_up`='{$now}' where `id`='{$id}' limit 1";
			$this->m->query($query);
		}
		return true;
	}
	
	public function mylist($condition=array(),$extra=''){
        $return=array();
        $query="select `id`,`tag`,`hits` from `{$this->table_index}` where 1 {$extra} order by `id` desc";
        $total=$this->_total($query);
		$ret=$this->_page($total,10);
		$pager=$this->pager($ret);
        
        if($total>0){
            $query=$query." limit {$ret['c']}";
            $return[]=$this->m->mselect($query);
            $return[]=$pager;
        }
        return $return;
    }
	//统计word标签的文章数量
	private function c($tag){
		//做缓存
		$tag=addslashes($tag);
		$query="select count(*) from `{$this->table_list}` where `tag`='{$tag}'";
		
		$db_cache=new db_cache($this->cache_dir,$query,$this->cache_time);
		$info=$db_cache->action('r');
		if(!$info){
			$info=$this->m->get_field($query);
			$db_cache->action('w',$info);
		}
		return $info;
	}
	public function arc_list($condition=array(),$extra=''){
        $return=array();
        $query="select `l`.`tag`,`l`.`sentence`,`a`.`id`,`a`.`level_id`,`a`.`title`,`a`.`add_time`,`a`.`hits`,`a`.`htmlurl` from `{$this->table_list}` `l` left join `{$this->table_arc}` `a` on `l`.`arc_id`=`a`.`id` where 1 {$extra} order by `a`.`id` desc";
        $total=$this->_total($query);
		$ret=$this->_page($total,10);
		$pager=$this->pager($ret);
        
        if($total>0){
            $query=$query." limit {$ret['c']}";
            $return[]=$this->m->mselect($query);
            $return[]=$pager;
        }
        return $return;
    }
	public function in_($tag,$n=12){
		//'static_rule_list_index'=>array('/html/{level_id}/index.html',ROOT.'html/{level_id}/index.html'),
		list($linkurl,)=$this->conf->static_rule_list_index;
		$query="select distinct `a`.`level_id` from `{$this->table_list}` `l` left join `{$this->table_arc}` `a` on `l`.`arc_id`=`a`.`id` where 1 and `l`.`tag`='{$tag}' and `a`.`status`='1' order by `l`.`level_id` asc limit {$n}";
		$result=$this->m->query($query);
		while(false!==$row=$this->m->fetch_assoc($result)){
			$row['linkurl']=str_replace('{level_id}',$row['level_id'],$linkurl);
			$d[]=$row;
		}
		return $d;
	}
//
}